/**
 * @author <a href="http://www.pixiv.net/">pixiv</a>
 * @example
 * <script src="http://source.pixiv.net/source/embed.js" data-id="embedId" data-size="small|medium|large" data-border="on|off" charset="utf-8"></script>
 */
new function() {
var script, options, element,
	id = 'pixiv-embed-'+Math.random();
script = document.getElementsByTagName('script');
script = script[script.length - 1];
options = {
	id    : script.getAttribute('data-id'),
	size  : script.getAttribute('data-size'),
	border: "off"
};
if (!options.id || !options.size) return;initialize();showEmbed();
function initialize() {
	var handler;
	if (!window.__pixiv__)
		window.__pixiv__ = {
			url  : 'http://embed.pixiv.net',
			sizes: {large : [670, 550],	medium: [360, 300],	small : [190, 250]},
			addedMessageEvent: false
		};
}
function showEmbed() {
	var script = document.createElement('script');
	var q = {
		callback : '__pixiv__["'+id+'"]',
		id       : options.id,
		size     : options.size
	};
	script.src = __pixiv__.url+'/embed_json.php?'+param(q);script.charset = 'utf-8';document.getElementsByTagName('head')[0].appendChild(script);
	__pixiv__[id] = showEmbedHandler;
}
function showEmbedHandler(data) {
	var url = __pixiv__.url+'/embed_mk2.php?'+param(options),
		width = data.img_w + 10;
	var div = document.createElement('div');
	div.className = 'pixiv-embed-container';
	div.innerHTML = [
		'<div class="pixiv-embed" style="text-align:center;">',
		'<p class="pixiv-embed-illust" style="width:'+width+'px; margin:0 auto; padding:5px 0; text-align:center; background-color:#dde6ee;">',
		'<iframe src="'+url+'" width="'+data.img_w+'" height="'+data.img_h+'" frameborder="0" style="vertical-align:middle; border:none;"></iframe>',
		'</p>',
		'<p class="pixiv-embed-title" style="margin:0;">',
		'<a href="http://www.pixiv.net/member_illust.php?mode=medium&amp;illust_id='+data.img_id+'" target="_blank">',
		escapeHTML(data.title),
		'</a>',
		'<span class="pixiv-embed-author">',
		'by <a href="http://www.pixiv.net/member.php?id='+data.user_id+'" target="_blank">',
		escapeHTML(data.user),
		'</a> ',
		'</span>',
		'<span class="pixiv-embed-datetime">'+escapeHTML(data.update)+'</span> ',
		'on <a href="http://www.pixiv.net/" target="_blank">pixiv</a>',
		'</p>',
		'</div>'
	].join('');
	script.parentNode.insertBefore(div, script);
	delete __pixiv__[id];
}
function param(queries) {var k, ret = [];for (k in queries)ret.push(k+'='+queries[k]);return ret.join('&');}
function escapeHTML(str) {return str.toString().replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');}
};

